﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using CMISLibrary.Entities;
using BCNPCObject.DB;

namespace BCNPCObject
{
    public class cls_D_TRAM_Controller
    {
        #region   Atributes

        private DataSet CMIS_Header = new CMISOutputParameter();
        private DataSet ds = new DataSet();
        private DataTable dt = new DataTable();
        private D_TRAM info;

        #endregion

        #region   Constructor

        public cls_D_TRAM_Controller()
        {
            info = new D_TRAM();
        }
        public cls_D_TRAM_Controller(D_TRAM Info)
        {
            this.info = Info;
        }

        #endregion

        #region   Properties

        public D_TRAM pInfor
        {
            get { return info; }
            set { info = value; }
        }

        #endregion

        #region   Methods

        public IEnumerable<D_TRAM> select_D_TRAM()
        {
            try
            {
                var q = from p in CMIS2.DB.D_TRAM select p;
                return q;
            }
            catch
            {
                CMIS2.ResetDB(); return null;
            }
        }
        public IEnumerable<D_TRAM> select_D_TRAM(string strMadviqly)
        {
            try
            {
                var q = from p in CMIS2.DB.D_TRAM where p.MA_DVIQLY == strMadviqly orderby p.MA_TRAM select p;
                return q;
            }
            catch
            {
                CMIS2.ResetDB(); return null;
            }
        }
        public bool insert_D_TRAM()
        {
            try
            {
                D_TRAM objD_TRAM = new D_TRAM()
                {
                    CSUAT_TRAM = info.CSUAT_TRAM,
                    LOAI_TRAM = info.LOAI_TRAM,
                    MA_CAPDA = info.MA_CAPDA,
                    MA_CAPDA_RA = info.MA_CAPDA_RA,
                    MA_DVIQLY = info.MA_DVIQLY,
                    MA_TO = info.MA_TO,
                    MA_TRAM = info.MA_TRAM,
                    TEN_TRAM = info.TEN_TRAM,
                    TINH_TRANG = info.TINH_TRANG
                };
                CMIS2.DB.D_TRAM.InsertOnSubmit(objD_TRAM);
                CMIS2.DB.SubmitChanges();
                CMIS2.ResetDB();
                return true;
            }
            catch
            {
                CMIS2.ResetDB(); return false;
            }
        }
        
        public IEnumerable<D_TRAM> selectWithPK_D_TRAM()
        {
            try
            {
                var q = from p in CMIS2.DB.D_TRAM where p.MA_DVIQLY == info.MA_DVIQLY && p.MA_TRAM == info.MA_TRAM select p;
                return q;
            }
            catch
            {
                CMIS2.ResetDB(); return null;
            }
        }
        public IEnumerable<D_TRAM> select_D_TRAM_ByPK(System.String pr_MA_DVIQLY, System.String pr_MA_TRAM)
        {
            try
            {
                var q = from p in CMIS2.DB.D_TRAM where p.MA_DVIQLY == pr_MA_DVIQLY && p.MA_TRAM == pr_MA_TRAM select p;
                return q;
            }
            catch
            {
                CMIS2.ResetDB(); return null;
            }
        }
        public bool update_D_TRAM()
        {
            try
            {
                D_TRAM objD_TRAM = CMIS2.DB.D_TRAM.Single(c => c.MA_DVIQLY == info.MA_DVIQLY && c.MA_TRAM == info.MA_TRAM);
                objD_TRAM.CSUAT_TRAM = info.CSUAT_TRAM;
                objD_TRAM.LOAI_TRAM = info.LOAI_TRAM;
                objD_TRAM.MA_CAPDA = info.MA_CAPDA;
                objD_TRAM.MA_CAPDA_RA = info.MA_CAPDA_RA;
                objD_TRAM.MA_TO = info.MA_TO;
                objD_TRAM.TEN_TRAM = info.TEN_TRAM;
                objD_TRAM.TINH_TRANG = info.TINH_TRANG;
                CMIS2.DB.SubmitChanges();
                return true;
            }
            catch
            {
                CMIS2.ResetDB(); return false;
            }
        }
        public bool delete_D_TRAM()
        {
            try
            {
                var query = from p in CMIS2.DB.D_TRAM where p.MA_DVIQLY == info.MA_DVIQLY && p.MA_TRAM == info.MA_TRAM select p;
                if (query != null && query.Count() > 0)
                {
                    CMIS2.DB.D_TRAM.DeleteAllOnSubmit(query.ToList());
                    CMIS2.DB.SubmitChanges();
                    return true;
                }
                return true;
            }
            catch
            {
                CMIS2.ResetDB(); return false;
            }
        }
        public bool delete_D_TRAM_ByPK(System.String pr_MA_DVIQLY, System.String pr_MA_TRAM)
        {
            try
            {
                var query = from p in CMIS2.DB.D_TRAM where p.MA_DVIQLY == pr_MA_DVIQLY && p.MA_TRAM == pr_MA_TRAM select p;
                if (query != null && query.Count() > 0)
                {
                    CMIS2.DB.D_TRAM.DeleteAllOnSubmit(query.ToList());
                    CMIS2.DB.SubmitChanges();
                    return true;
                }
                return true;
            }
            catch
            {
                CMIS2.ResetDB(); return false;
            }
        }

        #endregion
        #region METHOD DUNGNT PC1
        public IEnumerable<D_TRAM> Get_D_TRAM(string strMadviqly)
        {
            try
            {
                var q = from p in CMIS2.DB.D_TRAM where p.MA_DVIQLY == strMadviqly orderby p.MA_TRAM select p;
                return q;
            }
            catch
            {
                CMIS2.ResetDB(); return null;
            }
        }
        # endregion
    }
}
